<template>
	<view class="main">

		<!-- 顶部自定义导航 -->
		<!-- <minitemtag></minitemtag> -->
		<minitemtag v-on:pChangeType="changeType"></minitemtag>
		<!-- 顶部选项卡 -->
		<view class="tab-frame">
			<scroll-view id="nav-bar" class="nav-bar" scroll-x scroll-with-animation :scroll-left="scrollLeft">
				<view v-for="(item,index) in tabBars" :key="item.channelId" class="nav-item" :class="{current: index === tabCurrentIndex}"
				 :id="'tab'+index" @click="changeTab(index,item.channelId)">{{item.channelName}}</view>
			</scroll-view>
		</view>
		<!-- 下拉刷新组件 -->
		<mix-pulldown-refresh ref="mixPulldownRefresh" class="panel-content" :top="235" @refresh="onPulldownReresh"
		 @setEnableScroll="setEnableScroll"> 
			<!-- 内容部分的 --> 
			<swiper id="swiper" class="swiper-box" :duration="300" :current="tabCurrentIndex" @change="changeTab">
				<swiper-item v-for="(tabItem,index) in tabBars" :key="index">
					<scroll-view class="panel-scroll-box" :scroll-y="enableScroll" @scrolltolower="loadMore">
						<u-empty v-if="tabItem.newsList.length==0" :text="$t('video.listempty')" mode="list"></u-empty>
						<!-- 短视频 -->
						<view v-if="index==0" class="shortVideo"> 
							<view class="duan-frame" :style="{background: 'url('+'http://pic-asean2.oss-cn-hongkong.aliyuncs.com/'+item.video.img_url+')'}" v-for="(item, i) in tabItem.newsList" :key="i" @click="watchClick(0,item.news_id)">
								<!-- <image :src="'http://pic-asean2.oss-cn-hongkong.aliyuncs.com/'+item.video.img_url" mode="aspectFill"></image> -->
								<view class="duan-text">
									<view>{{item.title}}</view>
									<view>
										<u-icon name="play-right-fill" color="#ffffff" size="28"></u-icon> {{item.view_count}} {{$t('video.play')}}
									</view>
								</view>
								<view class="zian">{{item.praise_count}} {{$t('video.fabulous')}}</view>
							</view>
						</view>
						<!-- 直播 -->
						<view v-if="index==1" v-for="(item, i) in tabItem.newsList" :key="i" class="video-frame">
							<view class="video-content">
								<view class="frame_img zwyPortrait" style="--color:#ed4141;">
									<image v-if="item.room_user.avatar_url" :src="item.room_user.avatar_url" mode=""></image>
									<image v-if="!item.room_user.avatar_url" src="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1575568780,3963509117&fm=15&gp=0.jpg"
									 mode=""></image>
								</view>
								<view class="frame_tltle">
									<view>{{item.room_user.name}}</view>
									<text>{{item.room_user.fans_count}} {{$t('video.fans')}}</text>
								</view>
								<button @click="followClock(item,2)" style="padding: 10rpx 20rpx;" type="error" size="medium">{{item.follow===true?$t('video.concerned'):$t('video.follow')}}</button>
							</view>
							<view class="frame-trxt"><text>{{item.room_name}}</text> {{item.room_describe}}</view>
							<view class="forame-image" @click="watchClick(1,item.room_id)">
								<image :src="item.room_cover" mode=""></image>
								<view class="frame-watch">
									<view class="watchzb">
										<view style="display: inherit;">
											<view style="width: 50rpx;height: 50rpx;display: flex;align-items: center;justify-content: center;">
												<view class="zwyLive" style="--color:white;"></view>
											</view>
										</view>
										<text>{{$t('video.live')}}</text>
									</view>
									<text>{{item.view_count}}{{$t('video.people')}}</text>
								</view>
							</view>
						</view>
						<!-- 访谈 -->
						<view v-if="index==2" class="interview">
							<view v-for="(item, i) in tabItem.newsList" :key="i">
							<view class="interview-img" @click="watchClick(2,item.news_id)">
								<!-- <video @play="saveplay" src="http://ali.cdn.kaiyanapp.com/a5baa889a633d81f7a4967057bcc826f_1280x720_854x480.mp4?auth_key=1596513576-0-0-34ff19ab82702c6388b2bb3e38797942" controls></video> -->
								<image :src="'http://pic-asean2.oss-cn-hongkong.aliyuncs.com/'+item.video.img_url" mode=""></image>
								<view class="interview-title">{{item.title}}</view>
								<view class="interview-play">{{item.view_count}}{{$t('video.play')}}</view>
								<view class="interview-time"> {{s_to_hs(item.video.duration)}}</view>
							</view>
							<view class="interview-content" >
								<view class="content-info">
									<view class="frame_img" >
										<image :src="item.media_user.avatar_url" mode=""></image>
										<!-- <image src="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1575568780,3963509117&fm=15&gp=0.jpg" mode=""></image> -->
									</view>
									<text>{{item.media_user.name}}</text>
									<text @click="followClock(item,3)" style="padding: 10rpx 20rpx;color: #ed4141;">{{item.follow===true?$t('video.concerned'):$t('video.follow')}}</text>
								</view>
								<view style="margin-top: 20rpx;">
									<u-icon :label="item.comment_count" size="50" name="chat"></u-icon>
								</view>
							</view> 
						</view>
						</view>
						<!-- 上滑加载更多组件 -->
						<mix-load-more :status="tabItem.loadMoreStatus"></mix-load-more>
					</scroll-view>
				</swiper-item>
			</swiper>
		</mix-pulldown-refresh>
		<u-toast ref="uToast" />
	</view>

	</view>
</template>

<script>
	import utils from '@/components/shoyu-date/utils.filter.js';
	import minitemtag from '../index/game.vue';
	import mixAdvert from '@/components/mix-advert/vue/mix-advert';
	import mixPulldownRefresh from '@/components/mix-pulldown-refresh/mix-pulldown-refresh';
	import mixLoadMore from '@/components/mix-load-more/mix-load-more';
	import {
		getLanguage
	} from '@/util/tokeCookien.js'
	let windowWidth = 0,
		scrollTimer = false,
		tabBar;
		var	enchenj='zh-CN';
	export default {
		components: {
			mixPulldownRefresh,
			mixLoadMore,
			mixAdvert,
			minitemtag
		},
		data() {
			return {
				editcomplete: false, //编辑/完成
				listChannel: false, //底部侧频道列表展示
				utils: utils, //时间转换
				tabCurrentIndex: 0, //当前选项卡索引
				scrollLeft: 0, //顶部选项卡左滑距离
				enableScroll: true,
				tabBars: [],
				page_index: 1 //当前页数
			}
		},
		onLoad() {
			this.loadTabbars();
		},
		onShow() {
			// 获取屏幕宽度
			windowWidth = uni.getSystemInfoSync().windowWidth;
			if(enchenj!=this.$i18n.locale){
				enchenj=this.$i18n.locale
				this.loadTabbars();
			}
		},
		methods: { 
			//子组件调用父组件方法
			changeType() { 
				this.loadTabbars();
			},
			//跳转详情页面 
			watchClick(type,idx) { 
				if (type == 0) {//短视频
					uni.navigateTo({
						url: `/pages/videoclass/shortvideo/shortvideo?newsid=${idx}`
						// url: '/pages/videoclass/shortvideo/shortvideo'
					})
				} else if (type == 1) {//直播
					uni.navigateTo({
						url: `/pages/livedetails/livedetails?roomid=${idx}`
					})
				}else if (type == 2) {//访谈
					uni.navigateTo({
						url: `/pages/video/interviewdetails/interviewdetails?newsid=${idx}`
					})
				}
			},
			//关注
			async followClock(item,type) {
				let ids=0
				type==2?ids=item.room_user.user_id:ids=item.media_user.user_id
				const res = await this.$myRequest({
					url: '/user/follow',
					method: 'post',
					header: {},
					data: {
						up_uid: ids
					}
				})
				let operation = ''
				res.data.operation == 1 ? operation = this.$i18n.messages[this.$i18n.locale].pressrelease.Focusok : operation = this.$i18n.messages[this.$i18n.locale].pressrelease.CancelFocus
				this.$refs.uToast.show({
					title: operation,
					type: 'success'
				})
				item.follow = !item.follow
			},

			//设置分类
			async loadTabbars() {
				let tabList = []
				if (getLanguage() == 2) {
					tabList = [{
						channelId: 1,
						channelName: 'ShortVideo'
					}, {
						channelId: 2,
						channelName: 'LiveBroadcast'
					}, {
						channelId: 3,
						channelName: 'Interview'
					}, ]
				} else {
					tabList = [{
						channelId: 1,
						channelName: '短视频'
					}, {
						channelId: 2,
						channelName: '直播'
					}, {
						channelId: 3,
						channelName: '访谈'
					}, ]
				}
				tabList.forEach(item => {
					item.showdel = false
					item.newsList = [];
					item.page_index = 1;
					item.loadMoreStatus = 0; //加载更多 0加载前，1加载中，2没有更多了
					item.refreshing = 0; //动画
				})
				this.tabBars = tabList;
				this.loadNewsList('add');
			},
			//直播列表
			async loadNewsList(type) {
				let tabItem = this.tabBars[this.tabCurrentIndex];
				let channel_id = tabItem.channelId //频道id
				let page_size = 6 //每页显示多少条数据
				if (type === 'refresh') {
					tabItem.newsList = []; //刷新前清空数组
					this.tabBars[this.tabCurrentIndex].page_index = 1
				}
				let page_index = tabItem.page_index //当前第几页
				var list_URL = ''
				if (this.tabCurrentIndex == 1) { //直播
					list_URL = '/live/get-liveroom-list/' + channel_id + '/' + page_size + '/' + page_index
				} else if (this.tabCurrentIndex == 2) { //访谈
					list_URL = '/news/get-video-list/' + page_size + '/' + page_index
				} else if (this.tabCurrentIndex == 0) { //短视频
					list_URL = '/news/get-video-mini-list/' + page_size + '/' + page_index
				}
				const res = await this.$myRequest({
					url: list_URL,
					header: {}
				})
				//type add 加载更多 refresh下拉刷新
				if (type === 'add') {
					if (tabItem.loadMoreStatus === 2) {
						return;
					}
					tabItem.loadMoreStatus = 1;
				}
				// #ifdef APP-PLUS
				else if (type === 'refresh') {
					tabItem.refreshing = true;
				}
				// #endif

				//setTimeout模拟异步请求数据
				setTimeout(() => {
					let list = res.data;
					list.forEach(item => {
						item.comment_count=item.comment_count+''
						tabItem.newsList.push(item);
					}) 
					//下拉刷新 关闭刷新动画
					if (type === 'refresh') {
						this.$refs.mixPulldownRefresh && this.$refs.mixPulldownRefresh.endPulldownRefresh();
						// #ifdef APP-PLUS
						tabItem.refreshing = false;
						// #endif
						tabItem.loadMoreStatus = 0;
					}
					//上滑加载 处理状态
					if (type === 'add') {
						tabItem.loadMoreStatus = list.length == 0 ? 2 : 0;
						this.tabBars[this.tabCurrentIndex].page_index++
					}
				}, 600)
			},

			//下拉刷新
			onPulldownReresh() {
				this.loadNewsList('refresh');
			},
			//上滑加载
			loadMore() {
				this.loadNewsList('add');
			},
			//设置scroll-view是否允许滚动，在小程序里下拉刷新时避免列表可以滑动
			setEnableScroll(enable) {
				if (this.enableScroll !== enable) {
					this.enableScroll = enable;
				}
			},
			//计算时长
			s_to_hs(s) {
				//计算分钟
				//算法：将秒数除以60，然后下舍入，既得到分钟数
				var h;
				h = Math.floor(s / 60);
				//计算秒
				//算法：取得秒%60的余数，既得到秒数
				s = s % 60;
				//将变量转换为字符串
				h += '';
				s += '';
				//如果只有一位数，前面增加一个0
				h = (h.length == 1) ? '0' + h : h;
				s = (s.length == 1) ? '0' + s : s;
				return h + ':' + s;
			},
			//tab切换
			async changeTab(e) {
				this.listChannel = false
				if (scrollTimer) {
					//多次切换只执行最后一次
					clearTimeout(scrollTimer);
					scrollTimer = false;
				}
				let index = e;
				//e=number为点击切换，e=object为swiper滑动切换
				if (typeof e === 'object') {
					index = e.detail.current
				}
				if (typeof tabBar !== 'object') {
					tabBar = await this.getElSize("nav-bar")
				}
				//计算宽度相关
				let tabBarScrollLeft = tabBar.scrollLeft;
				let width = 0;
				let nowWidth = 0;
				//获取可滑动总宽度
				for (let i = 0; i <= index; i++) {
					let result = await this.getElSize('tab' + i);
					width += result.width;
					if (i === index) {
						nowWidth = result.width;
					}
				}
				if (typeof e === 'number') {
					//点击切换时先切换再滚动tabbar，避免同时切换视觉错位
					this.tabCurrentIndex = index;
				}
				//延迟10ms,等待swiper动画结束再修改tabbar
				scrollTimer = setTimeout(() => {
					if (width - nowWidth / 2 > windowWidth / 2) {
						//如果当前项越过中心点，将其放在屏幕中心
						this.scrollLeft = width - nowWidth / 2 - windowWidth / 2;
					} else {
						this.scrollLeft = 0;
					}
					if (typeof e === 'object') {
						this.tabCurrentIndex = index;
					}
					this.tabCurrentIndex = index;
					//第一次切换tab，动画结束后需要加载数据
					let tabItem = this.tabBars[this.tabCurrentIndex];
					if (this.tabCurrentIndex !== 0 && tabItem.loaded !== true) {
						this.loadNewsList('add');
						tabItem.loaded = true;
					}
				}, 10)

			},
			//获得元素的size
			getElSize(id) {
				return new Promise((res, rej) => {
					let el = uni.createSelectorQuery().select('#' + id);
					el.fields({
						size: true,
						scrollOffset: true,
						rect: true
					}, (data) => {
						res(data);
					}).exec();
				});
			},
		}
	}
</script>

<style lang='scss' scoped>
	page,
	.content {
		background-color: #e8e8e8;
		height: 100%;
		overflow: hidden;
	}

	.main {
		background-color: #f0f0f0;
	}

	/* 频道列表 */
	.channelTxt {
		width: 100%;
		padding: 100rpx 15rpx 10rpx 15rpx;

		view {
			color: #080808;
			font-size: 50rpx;
			position: relative;

			text {
				position: absolute;
				top: 26rpx;
				left: 228rpx;
				font-size: 25rpx;
				color: #cbcbcb;
			}

			.edit {
				position: absolute;
				top: 20rpx;
				right: 50rpx;
				color: #ed4141;
				font-size: 30rpx;
			}
		}
	}

	.channelTag {
		padding: 15rpx;
		display: inline-block;

		.tag-info {
			display: inline-block;
			float: left;
			margin: 15rpx 0 15rpx 30rpx;
		}
	}

	/* 顶部tabbar */
	.tab-frame {
		position: relative;

		.nab-icon {
			background-image: linear-gradient(90deg, rgba(255, 255, 255, 0.7), #ffffff);
			width: 13%;
			height: 90rpx;
			position: absolute;
			right: 0rpx;
			top: 0rpx;
			z-index: 10;

			.icon {
				margin: 17rpx;
				margin-right: 0rpx;
			}
		}
	}

	.nav-bar {
		position: relative;
		z-index: 10;
		height: 90upx;
		white-space: nowrap;
		width: 100%;
		box-shadow: 0 2upx 8upx rgba(0, 0, 0, .06);
		background-color: #fff;

		.nav-item {
			display: inline-block;
			width: 33.3333333%;
			height: 90upx;
			padding: 0 20rpx;
			text-align: center;
			line-height: 90upx;
			font-size: 30upx;
			color: #303133;
			position: relative;

			&:after {
				content: '';
				width: 0;
				height: 0;
				border-bottom: 4upx solid #ed4141;
				position: absolute;
				left: 50%;
				bottom: 0;
				transform: translateX(-50%);
				transition: .3s;
			}
		}

		.current {
			color: #ed4141;

			&:after {
				width: 50%;
			}
		}
	}

	.swiper-box {
		height: 100%;
	}

	.panel-scroll-box {
		height: 100%;

		.panel-item {
			background: #fff;
			padding: 30px 0;
			border-bottom: 2px solid #000;
		}
	}

	/**
	 * 直播列表 
	 */
	.video-frame {
		padding: 20rpx;
		background-color: #fff;
		height: 620rpx;
		margin-bottom: 20rpx;
		border-radius: 20rpx;

		.frame-trxt {
			font-size: 35rpx;
			padding-bottom: 10rpx;

			text {
				font-weight: 800;
				padding-right: 30rpx;
			}
		}

		.forame-image {
			position: relative;

			image {
				border-radius: 10rpx;
				width: 100%;
				height: 400rpx;
			}

			.frame-watch {
				position: absolute;
				padding-right: 20rpx;
				top: 20rpx;
				left: 20rpx;
				height: 60rpx;
				border-radius: 15rpx;
				font-size: 25rpx;
				line-height: 60rpx;
				color: #fff;
				background-color: rgba($color: #000000, $alpha: 0.4);

				text {
					margin-left: 20rpx;
				}

				.watchzb {
					border-radius: 15rpx;
					display: inline-block;
					background-color: #ed4141;
					padding: 0 10rpx;

					text {
						margin: 0;
					}
				}


			}
		}


		.video-content {
			width: 100%;
			margin-bottom: 30rpx;
			display: flex;
			font-size: 26rpx;
			color: #909399;
			justify-content: center; //子元素水平居中
			align-items: center; //子元素垂直居中

			.frame_img {
				flex: 1;
				width: 90rpx;
				height: 90rpx;
				border-radius: 50px;

				image {
					border-radius: 50px;
					width: 90rpx;
					height: 90rpx;
				}
			}

			.frame_tltle {
				margin-left: 30rpx;
				flex: 5;

				text {
					margin-right: 16rpx;
				}

				view {
					font-weight: 600;
					font-size: 30rpx;
					color: #000000;
				}
			}

			button {
				flex: 2;
				height: 60rpx;
				line-height: 45rpx;
				font-size: 30rpx;
				background-color: #ed4141;
				color: #fff;
			}
		}
	}

	/* 短视频列表 */
	.shortVideo {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;

		.duan-frame {
			color: #fff;
			position: relative;
			width: 50%;
			height: 500rpx;
			overflow: hidden;
			background-size: cover;
			background-position: center;
			image {
				width: 100%;
				height: 500rpx;
			}

			/* 视频内文字 */
			.duan-text {
				position: absolute;
				padding-right: 20rpx;
				bottom: 20rpx;
				left: 20rpx;
				font-size: 30rpx;
				font-weight: 600;

			}

			.zian {
				position: absolute;
				bottom: 20rpx;
				right: 20rpx;
				font-size: 30rpx;
			}
		}
	}

	/* 访谈 */
	.interview {
		.interview-img {
			position: relative;
			height: 400rpx;

			image {
				width: 100%;
				height: 400rpx;
			}
video {
				width: 750rpx;
				height: 400rpx;
			}
			.interview-title {
				position: absolute;
				top: 20rpx;
				left: 20rpx;
				padding-right: 20rpx;
				color: #fff;
				font-size: 35rpx;
			}

			.interview-play {
				position: absolute;
				bottom: 30rpx;
				left: 20rpx;
				color: #fff;
			}

			.interview-time {
				position: absolute;
				bottom: 20rpx;
				right: 20rpx;
				color: #fff;
				font-size: 25rpx;
				padding: 8rpx 20rpx;
				border-radius: 30rpx;
				background-color: rgba(0, 0, 0, .3);
			}
		}

		.interview-content {
			background-color: #fff;
			display: flex;
			justify-content: space-between;
			padding: 10rpx 30rpx;

			.content-info {
				width: 100%;
				display: flex;
				font-size: 26rpx;
				color: #909399;
				align-items: center; //子元素垂直居中

				.frame_img {
					width: 90rpx;
					height: 90rpx;
					border-radius: 50px;

					image {
						border-radius: 50px;
						width: 90rpx;
						height: 90rpx;
					}
				}

				text {
					margin-left: 20rpx;
					font-size: 30rpx;
					color: #000000;
				}
			}
		}
	}
</style>
